//
//  WBNavigationController.swift
//  Weibo
//
//  Created by Albert Zhu on 16/9/28.
//  Copyright © 2016年 Albert Zhu. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationBar.isHidden = true
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if viewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            
            var title = "返回"
        
            if let vc = viewController as? WBBaseViewController {
                
                if viewControllers.count == 1 {
                    title = childViewControllers.first?.title ?? "返回"
                }
                
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent))
            }

        }
        
        super.pushViewController(viewController, animated: animated)
    }
    
    @objc private func popToParent() {
        
        popViewController(animated: true)
    }
}
